<?php
function getStrikeTemp($grain_temp,$target_temp,$water_to_grain) {
	$a = .2/$water_to_grain;
	$b = $target_temp - $grain_temp;
	$c = $a * $b;
	$d = $c + $target_temp;
	return $d;
}

function getRealTemp($probe) {
        include("sql.php.inc");
	$sql = "SELECT real_temp from temps where temp_index=\"$probe\"";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
	return $row[0];

}

function isOff($probe) {
        include("sql.php.inc");
	$sql = "SELECT off from temps where temp_index=\"$probe\"";
	$result = mysql_query($sql,$connect);
	if (mysql_num_rows($result) == 0) {
		return true;
	} else {
		$row = mysql_fetch_row($result);
		return $row[0];
	}
}

function setOff($probe) {
        include("sql.php.inc");
	$sql = "UPDATE temps set off=1 where temp_index=\"$probe\"";
        $r = mysql_query($sql,$connect);

}

function setOn($probe) {
        include("sql.php.inc");
	$sql = "UPDATE temps set off=0 where temp_index=\"$probe\"";
        $r = mysql_query($sql,$connect);
}

function setHltProbe($probe) {
        include("sql.php.inc");
	$sql = "SELECT count(*) from hltmash";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
	if ($row[0] >= 1) {
		$sql = "UPDATE hltmash set hlt_probe=\"$probe\"";
		$r = mysql_query($sql,$connect);
	} else {
		$sql = "INSERT into hltmash (hlt_probe) values (\"$probe\")";
		$r = mysql_query($sql,$connect);
	}
}

function setMashProbe($probe) {
        include("sql.php.inc");
        $sql = "SELECT count(*) from hltmash";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
        if ($row[0] >= 1) {
                $sql = "UPDATE hltmash set mash_probe=\"$probe\"";
		$r = mysql_query($sql,$connect);
        } else {
                $sql = "INSERT into hltmash (mash_probe) values (\"$probe\")";
		$r = mysql_query($sql,$connect);
        }
}

function isHLT($probe) {
        include("sql.php.inc");
	$sql = "SELECT count(*) from hltmash where hlt_probe=\"$probe\"";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
	if ($row[0] >= 1) {
		return true;
	} else {
		return false;
	}
	
}

function isMASH($probe) {
        include("sql.php.inc");
	$sql = "SELECT count(*) from hltmash where mash_probe=\"$probe\"";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
	if ($row[0] >= 1) {
		return true;
	} else {
		return false;
	}
	
}

function getOG($probe) {
        include("sql.php.inc");
        $sql = "SELECT og from temps where temp_index=\"$probe\"";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
	return ($row[0]);

}

function setOG($probe,$og) {
        include("sql.php.inc");
        $sql = "UPDATE temps set og=\"$og\" where temp_index=\"$probe\"";
        $r = mysql_query($sql,$connect) or die (mysql_error());
}

function getHasTemp($probe) {
        include("sql.php.inc");
        $sql = "SELECT count(*) from temps where temp_index=\"$probe\"";
        $r = mysql_query($sql,$connect);
        $row = mysql_fetch_row($r);
	return ($row[0]);
}
function setName($probe,$beer_name) {
	include("sql.php.inc");
	if (getHasTemp($probe) == 0) {
		$sql = "INSERT into temps (temp_index,name) values ($probe,\"$beer_name\")";
		$r = mysql_query($sql,$connect);
	} else {
		$sql = "UPDATE temps set name=\"$beer_name\" where temp_index=$probe";
		$r = mysql_query($sql,$connect);
	}
}
function setVariance($probe,$variance) {
	include("sql.php.inc");
	if (getHasTemp($probe) == 0) {
		$sql = "INSERT into temps (temp_index,variance) values ($probe,\"$variance\")";
		$r = mysql_query($sql,$connect);
	} else {
		$sql = "UPDATE temps set variance=\"$variance\" where temp_index=$probe";
		$r = mysql_query($sql,$connect);
	}
}

function setTargetTemp($probe,$temp) {
	include("sql.php.inc");
	if (getHasTemp($probe) == 0) {
		$sql = "INSERT into temps (temp_index,temp) values ($probe,\"$temp\")";
		$r = mysql_query($sql,$connect);
	} else {
		$sql = "UPDATE temps set temp=\"$temp\" where temp_index=$probe";
		$r = mysql_query($sql,$connect);
	}
}

function getName($probe) {
	include("sql.php.inc");
	$sql = "SELECT name from temps where temp_index=\"$probe\"";
	$r = mysql_query($sql,$connect);
	$row = mysql_fetch_row($r);
	return $row[0];
}

function targetTemp($probe) {
	include("sql.php.inc");
	$sql = "SELECT temp from temps where temp_index=\"$probe\"";
	$r = mysql_query($sql,$connect);
	$row = mysql_fetch_row($r);
	return $row[0];
}

function variance($probe) {
	include("sql.php.inc");
	$sql = "SELECT variance from temps where temp_index=\"$probe\"";
	$r = mysql_query($sql,$connect);
	$row = mysql_fetch_row($r);
	return $row[0];
}

function heatOn($probe) {
	include("sql.php.inc");
	$gpio=-1;
	$sql = "SELECT gpio from gpiotoheat where temp_index=$probe";
	$r = mysql_query($sql,$connect);
	$row = mysql_fetch_row($r);
	if ($row[0] != "") {
		$gpio = $row[0];
	}
	
	if ($gpio > -1) {
		$f = file("/sys/devices/virtual/gpio/gpio".$gpio."/value");
		if ($f[0] == 0) {
			$r = "No";
		} else {
			$r = "Yes";
		}
		return $r;
	}
}

function getLinks() {
	$lines = file("/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves");
	return(sizeof($lines));
}

function getTemp($probe) {
$OK = false;
$lines = file("/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves");
for ($i = 0;$i<sizeof($lines); $i++) {
	$lines[$i] = trim($lines[$i]);
	$o = $i;
	$o++;
	if ($o == $probe) {
		//echo $lines[$i] ."\n";
                $reading = file("/sys/bus/w1/devices/".$lines[$i]."/w1_slave");
                foreach($reading as $r) {
                        $r = trim($r);
                        if ($OK) {
                                $temp = explode("=",$r);
                                $temp = $temp[1];
                                $temp = $temp/1000;
                                $temp = ($temp*1.8000)+32.00;
                                $temp = round($temp,2);
				return $temp ."\n";				
			}
                        if (strstr($r,"YES") && $OK==false) {
                                $OK = true;
                                //echo $r ."\n";
                        }  
		}
	}
}
}
?>
